import 'package:flutter/material.dart';
import 'package:flutter_hsh/utils/k_log.dart';
import 'package:get/get.dart';
import '../../fluttergromore/ads/ads_const.dart';
import '../../fluttergromore/ads/ads_utils.dart';
import '../../fluttergromore/config/app_const.dart';
import '../../fluttergromore/event_bus/event_bus_utils.dart';
import '../../fluttergromore/privacy/privacy_widget.dart';
import '../../fluttergromore/usersp/user_sp.dart';
import '../../fluttergromore/utils/dialog_utils.dart';
import 'navi_model.dart';

const String idPage = 'page';
const String idBottomBar = 'bottomBar';
const String idUserInfo = 'userInfo';

class MainLogic extends GetxController with WidgetsBindingObserver {
  List<bool> interstitial = [true, false, false];
  List<bool> feedShow = [true, false, false];

  List<NaviModel> bottomList = [
    NaviModel(
      title: '首页',
      icon: 'assets/images/nav_home.svg',
    ),
    NaviModel(
      title: '节气',
      icon: 'assets/images/nav_jieqi.svg',
    ),
    NaviModel(
      title: '记录',
      icon: 'assets/images/nav_jilu.svg',
    ),
    NaviModel(
      title: '热量',
      icon: 'assets/images/nav_reliang.svg',
    ),
    NaviModel(
      title: '我的',
      icon: 'assets/images/nav_mine.svg',
    ),
  ];
  int selectedIndex = 0;


  @override
  void onReady() {
    super.onReady();
    //如果是CSJ
    if (AppConst.isCSJ) {
      AdsUtils.showInterstitialAdCSJ();
    } else {
      initVip();
    }
  }

  void initVip() async {
    //是否第一次进入首页
    var isFirstInHome = await UserSp.instance.isFirstInHome();
    if (isFirstInHome) {
      //会员弹窗是否弹过
      var isShowVipDialog = await UserSp.instance.isShowVipDialog();
      if (isShowVipDialog) {
        //广告如果关闭
        if (!AdsConst.isOpenAds) {
          showPrivacy();
        } else {
          DialogUtils.showVipGet().then((type) {
            if (type == "c") {
              eventBusUtils.cpSucceedOn((arg) async {
                //移除监听
                eventBusUtils.cpSucceedOff();
                showPrivacy();
              });
              AdsUtils.showInterstitialAd();
            } else if (type == "j") {
              eventBusUtils.jlspSucceedOn((arg) async {
                //移除监听
                eventBusUtils.jlspSucceedOff();
                showPrivacy();
              });
              AdsUtils.showRewardVideoAd();
            }
          });
          UserSp.instance.putIsShowVipDialog(false);
        }
      }
    } else {
      //再次启动 进入首页展示2个插屏（展示开屏时预加载插屏，第一个插屏关闭后再展示第二个插屏）
      eventBusUtils.cpSucceedOnTow2((e) {
        KLog.d("cpSucceedOnTow2");
        AdsUtils.showInterstitialAd(isTime: false);
        eventBusUtils.cpSucceedOffTow2();
      });
      AdsUtils.showInterstitialAd(isTime: false);
    }
    UserSp.instance.putIsFirstInHome(false);
  }

  //隐私弹窗
  void showPrivacy() async {
    bool isAgreePrivacy = await UserSp.instance.isPrivacyAccept();
    if (!isAgreePrivacy) {
      return await Get.dialog(
        barrierDismissible: false,
        PrivacyWidget(
          onTapNo: () {
            AdsUtils.showInterstitialAd();
          },
          onTapYes: () {
            AdsUtils.showInterstitialAd();
            UserSp.instance.putPrivacyAccept(true);
          },
        ),
      );
    }
  }

  //切换Tab
  void switchTab(int i) {
    if (selectedIndex == i) {
      return;
    }
    selectedIndex = i;
    update([idPage, idBottomBar]);
    AdsUtils.showInterstitialAd();
  }

}
